package acom.test.swing.bb;

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class DisplayPanel extends JPanel {
	
	private static List<AnswerPeople> peoples = new ArrayList<AnswerPeople>();
	private static List<Prize> prizes = new ArrayList<Prize>();
	
	private Prize prize;
	
	private static List<AnswerPeople> tempPeoples = new ArrayList<AnswerPeople>();
	
	private static final long serialVersionUID = 1L;	

	private boolean Gaming;
		
	
	public DisplayPanel(){
						
		this.setBackground(Color.WHITE);
		
	}
	
	public void moveLable(final List<AnswerPeople> tempPeoples) {
		try {
			EventQueue.invokeLater(new Runnable() {
				public void run() {					
					
					Component[] comps = DisplayPanel.this.getComponents();
					for(int j=0;j<comps.length;j++){
						((JLabel)comps[j]).setText("");
					}
					for(int i=0;i<tempPeoples.size();i++){
						((JLabel)comps[i]).setText(tempPeoples.get(i).getPeopleName());
					}
					
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class GameDriver implements Runnable {

		@Override
		public void run() {
			
			peoples=SystemUtil.getPeoplelist();
			
			int viewLabelnum = SystemUtil.getViewLabelNum();
			
			while (Gaming) {
				for (int i=0; i<(peoples.size()-viewLabelnum)&& Gaming;i++) {
					tempPeoples.clear();
					for(int j=0;j<viewLabelnum;j++){
						tempPeoples.add(peoples.get(i+j));
					}
					
					
					moveLable(tempPeoples);					
					
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
			//循环 显示停止后 保存 抽中奖的人
			prize = SystemUtil.getCurPrize();
			prize.getPersons().addAll(tempPeoples);
			
			
			peoples.removeAll(tempPeoples);			
			
			if(prize.getPersons().size()>=prize.getTotal()){
				prize.setIsFinish(true);
			}
		}
	}

	public void start() {
		
		Gaming = true;
		new Thread(new GameDriver()).start();
	}

	public void stop() {
		Gaming = false;
	}

	public boolean isRunning() {
		return Gaming;
	}
	
}
